package com.hunanwenli.controller.login.findPwd;
import com.hunanwenli.tool.Tools;
import com.hunanwenli.service.login.FeignLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;



@Controller
public class FindPwdController {
    @Autowired
    private FeignLoginService feignLoginService;
    @RequestMapping(value = "/findPwd")
    public String findPwd(Model model, HttpServletRequest request){
        String token = Tools.makeToken();
        model.addAttribute("token",token);
        request.getSession().setAttribute("token",token);
        return "findPwd/addPhone";
    }

    @RequestMapping(value = "/findPwd2",method = RequestMethod.POST)
    public String findPwd2(String token,String phone,Model model, HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/findPwd";}
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
            token = Tools.makeToken();
            model.addAttribute("token",token);
            model.addAttribute("phone",phone);
            request.getSession().setAttribute("token",token);
        }else {
            return "forward:/findPwd";
        }

        return "findPwd/findPwd";
    }

    @RequestMapping(value = "/findPwd3",method = RequestMethod.POST)
    public String findPwd3(String token,String phone,Model model, HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/findPwd";}
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
            token = Tools.makeToken();
            model.addAttribute("phone",phone);
            model.addAttribute("token",token);
            request.getSession().setAttribute("token",token);
        }else {
            return "forward:/findPwd";
        }

        return "findPwd/findPwd2";
    }

    @RequestMapping(value = "/findPwd4",method = RequestMethod.POST)
    public String findPwd4(String token,String phone,String password,Model model, HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/findPwd";}
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
            token = Tools.makeToken();
            model.addAttribute("token",token);
            request.getSession().setAttribute("token",token);
        }else {
            return "forward:/findPwd";
        }
        Integer i = feignLoginService.findPwd4(phone, password);
        if (i==1){
            model.addAttribute("phone",phone);
            model.addAttribute("password",password);
        }

        return "findPwd/findPwd3";
    }


    @RequestMapping(value = "/ajaxFindPwd",method = RequestMethod.POST)
    @ResponseBody
    public String ajaxFindPwd(String phone,String email){
        return feignLoginService.ajaxFindPwd(phone,email);
    }

}
